<?php
class pUploadAction extends pBaseAction {

    public function _initialize() {
        parent::_initialize();
    }

    //接收来自于CKEditor的上传文件.file域的name=upload
    //不再将文件信息保存到数据库中
    public function ckImgUpload() {
        noCache();
        require THINK_PATH.'/Lib/ORG/Net/UploadFile.class.php';
        $uploader=new UploadFile();
        $uploader->savePath=UPLOAD_PATH.'media/';    //保存路径
        $uploader->allowExts=array('jpg','png','jpeg');
        $uploader->saveRule='uniqid';
        if( $uploader->upload() ) {
            $item=array_shift($uploader->getUploadFileInfo());
            $savePath=$item['savepath'].$item['savename'];  //获取保存后的路径
            //对图片进行缩略处理
            $this->thumb($savePath);
            $this->sendCKEditorResutl(__ROOT__.substr($savePath,1));
        }else {
            $this->sendCKEditorResutl('',$uploader->getErrorMsg());
        }
    }

    private function sendCKEditorResutl($url="",$error="") {
        echo<<<EOF
<script type="text/javascript">
   window.parent.CKEDITOR.tools.callFunction( {$_GET['CKEditorFuncNum']},"{$url}","{$error}");
</script>
EOF;
    }

    /**
     * 缩略图生成
     */
    private function thumb($imagePath) {
        if ( false == function_exists('gd_info') ) {  //没有GD库函数，无法进行图片缩放处理
            return;
        }
        $maxWidth=600;          //允许的最大图片宽度
        $imageSize=getimagesize($imagePath);
        if( $imageSize[0] > $maxWidth ) {    //图片宽度大于实际宽度，进行缩放处理
            import('ORG.Util.Image');
            Image::thumb($imagePath,'','',$maxWidth,$maxWidth*$imageSize[1]/$imageSize[0],true,'');
        }
    }

}